<?php /* Smarty version 2.6.20, created on 2012-05-16 12:21:28
         compiled from _core/pagecommon/_mvc/_control/handlepagebackendsample.tpl */ ?>
<?php require_once(SMARTY_CORE_DIR . 'core.load_plugins.php');
smarty_core_load_plugins(array('plugins' => array(array('modifier', 'escape', '_core/pagecommon/_mvc/_control/handlepagebackendsample.tpl', 66, false),)), $this); ?>
﻿function HandlePageBackendSample(gui, operator, sd, options) {
  this._gui = gui;
  this._operator = operator;
  this._sd = sd;
  this._options = options

  this._createElements();
};

HandlePageBackendSample.prototype._createElements = function() {
  this._loadData();
};

HandlePageBackendSample.prototype._loadData = function() {
  this._sample = null;
  this._retrieveSample();
};

HandlePageBackendSample.prototype._verifyData = function() {
  if (this._sample) {
    this._oldSample = MiscUtils.clone(this._sample);
    this._updateElements();
  }
};

HandlePageBackendSample.prototype._retrieveSample = function(page, condition, date, from, to, pos) {
  var _self = this;
  if (this._options.id) {
    /*new RequestUtils()._read('sample', null, 'd.oid = ' + this._options.id, null, null, null, null, function(result, params) { _self._sample = (result.data.length == 1) ? result.data[0] : null;
                                                                                                                               _self._verifyData.call(_self);
                                                                                                                             }, null);
*/
   var args = '&oid=' + this._options.id; 
  new RequestUtils()._mysql('v1sample', args, function(result, params) { _self._sample = (result.data.length == 1) ? result.data[0] : null;
                                                                          _self._verifyData.call(_self);
                                                                        }, {php:'customphp/mysqlread.php'});  

  } else {
    this._sample = v1sample.instance();
    this._sample.attributes = [];
    this._sample.attributes.options = [];
    this._verifyData();
  }
  
};

HandlePageBackendSample.prototype._updateElements = function() {
  var table, tr, td, _self = this;
  DlmanageUtils.clear(this._gui);
  
  this._gui.type.options[this._gui.type.options.length] = new Option('');
  for (var i = 0, il = SampleType.array().length; i < il; i++) {
    var type = SampleType.array()[i];
    option = new Option(type);
    this._gui.type.options[this._gui.type.options.length] = option;
    if (option.text == this._sample.type) {
      this._gui.type.selectedIndex = this._gui.type.options.length - 1;
    }
  }
  

  
  this._gui.model.options[this._gui.model.options.length] = new Option('');
  for (var i = 0, il = Manage.array().length; i < il; i++) {
    var model = Manage.array()[i];
    if (model.type != '<?php echo ((is_array($_tmp=@MODEL_TYPE_PRODUCT)) ? $this->_run_mod_handler('escape', true, $_tmp, 'javascript') : smarty_modifier_escape($_tmp, 'javascript')); ?>
') {
      continue;
    }
    option = new Option(model.title);
    option._model = model;
    this._gui.model.options[this._gui.model.options.length] = option;
    if (model.name == this._sample.model) {
      this._gui.model.selectedIndex = this._gui.model.options.length - 1;
    }
  }
  
  this._gui.name.value = this._sample.name;
  this._gui.price.value = this._sample.price;
  this._gui.unit.value = this._sample.unit;
  
   if (this._sample.isdisplay == 1) {
     this._gui.disnoRadio.checked = true;
  } else {
     this._gui.disyesRadio.checked = true;
  }   
  
  this._gui.name.onchange = function() { _self._sample.name = this.value; };
  
  this._gui.type.onchange = function() {if (_self._sample.attributes.length == 0) {
                                           _self._sample.type = this.options[this.selectedIndex].text;
                                           _self._sample.attributes = DlmanageUtils.attributes(_self._sample.type);
                                           _self._updateElements.call(_self);
                                         } else {
                                           _self._sample.type = this.options[this.selectedIndex].text;
                                           if (DlmanageUtils.attributes(_self._sample.type).length == 0 && _self._sample.attributes == DlmanageUtils.attributes(_self._sample.type)) {
                                             _self._updateElements.call(_self);
                                           } else {
                                             var r = window.confirm('CHANGE ATTRIBUTES?');
                                             if (r) {
                                               _self._sample.attributes = DlmanageUtils.attributes(_self._sample.type);
                                               _self._updateElements.call(_self);
                                             }
                                           }
                                         }
                                       };
  this._gui.model.onchange = function() { _self._sample.model = (this.options[this.selectedIndex]._model) ? this.options[this.selectedIndex]._model.name : ''; };
  this._gui.price.onchange = function() { _self._sample.price = this.value; };
  this._gui.unit.onchange = function() { _self._sample.unit = this.value; };
  
   this._gui.disyesRadio.onchange = function() { 
                                                if (_self._gui.disyesRadio.checked) 
                                                {
                                                  _self._sample.isdisplay = 0; 
                                                }
                                               };
  this._gui.disnoRadio.onchange = function() {   
                                               if (_self._gui.disnoRadio.checked) 
                                                {
                                                  _self._sample.isdisplay = 1; 
                                                }
                                             }; 
  
  
  this._gui.addAttribute.onclick = function() { 
                                               if(_self._sample.attributes.length == 0){
                                                 _self._sample.attributes[0] = v1sampleattribute.instance();
												  _self._sample.attributes[0].options = [];
                                                 
                                               }else{
                                                 _self._sample.attributes[_self._sample.attributes.length] = v1sampleattribute.instance();
												 _self._sample.attributes[_self._sample.attributes.length-1].options = [];
                                                 _self._sample.attributes[_self._sample.attributes.length-1].options[_self._sample.attributes[_self._sample.attributes.length-1].options.length] = v1sampleattributeoption.instance();
                                               }
                                                _self._updateElements.call(_self);
                                                return false;
                                              };
  
  table = document.createElement('table');
  table.cellPadding = 0;
  table.cellspacing = 0;
  this._gui.attributes.appendChild(table);
  
  if (this._sample.attributes.length == 0) {
    tr = table.insertRow(-1);
    td = tr.insertCell(-1);
    td.style.width = '900px';
    td.style.height = '40px';
    td.style.backgroundColor = '#fff';;
    td.style.textAlign = 'center';
    td.appendChild(document.createTextNode('N/A'));
  }
  for (var i = 0, il = this._sample.attributes.length; i < il; i++) {
    var attribute = this._sample.attributes[i];
    tr = table.insertRow(-1);
    td = tr.insertCell(-1);

    new IContainerSampleAttribute(td, attribute, { callbackFunc: function() { _self._updateElements.call(_self); }, index: i, parent: this._sample });
  }
  
  this._gui.save.onclick = function() { var str = '';
                                        if (_self._sample.name == '') {
                                          str += '名称, ';
                                        }
                                        if (_self._sample.type == '') {
                                          str += '类型, ';
                                        }
                                        if (_self._sample.name == '' || _self._sample.type == '') {
                                          window.alert(str.substring(0, str.length-2) + ' 不能为空');
                                        } else {
                                          if (_self._options.id) {
                                            _self._updateItems();
                                          } else {
                                              //function() { history.back(); }
                                            new RequestUtils()._mwrite('v1sample', [_self._sample], [], function() { history.back(); }, null);
                                          }
                                        }
                                       };
  this._gui.cancel.onclick = function() { history.back(); };
};


HandlePageBackendSample.prototype._updateItems = function() {
  var _self = this;
  
  this._modified = false;
  this._modifyAttributes = [];
  this._modifyOptions = [];
  this._deleteAttributes = [];
  this._deleteOptions = [];

  if (this._oldSample.type != this._sample.type || this._oldSample.model != this._sample.model || this._oldSample.unit != this._sample.unit) {
    this._modified = true;
  }
  
  DlmanageUtils.modifyAttr(this._oldSample, this._sample, this._modifyAttributes, this._modifyOptions, this._deleteAttributes, this._deleteOptions);
  
  this._loadDatas();
};

HandlePageBackendSample.prototype._loadDatas = function() {
  var _self = this;
  this._packageItems = null;
  this._purchaseItems = null;
  this._producePurchaseItems = null;
  
  if (this._modified || (this._modifyAttributes.length != 0 || this._modifyOptions.length != 0 || this._deleteAttributes.length != 0 || this._deleteOptions.length != 0)) {
    var r = window.confirm("IT WILL TAKE SOME TIME TO AFFECT ALL CUSTOMERS' PRODUCTS, CONTINUE?");
    if (r) {
      this._retrievePackageItems();
      this._retrievePurchaseItems();
      this._retrieveProducePurchaseItems();
    }
  } else {
    this._updateSample();
  }
};

HandlePageBackendSample.prototype._verifyItemData = function() {
  if (this._packageItems && this._purchaseItems && this._producePurchaseItems) {
    this._updateAllItems();
  }
};

HandlePageBackendSample.prototype._retrievePackageItems = function() {
  var _self = this;
  /*
  new RequestUtils()._read('packageitem', null, 'd.sample.oid=' + this._options.id, null, null, null, null, function(result, params) { _self._packageItems = result.data;
                                                                                                                                       _self._verifyItemData.call(_self);
                                                                                                                                     }, {pos: DOMUtils.findPos(this._gui.save)});
*/

   var args = '&oid=' + this._options.id; 
   new RequestUtils()._mysql('v1packageitem_s', args, function(result, params) { _self._packageItems = result.data;
                                                                          _self._verifyItemData.call(_self);
                                                                        }, {php:'customphp/mysqlread.php'});   
};

HandlePageBackendSample.prototype._retrievePurchaseItems = function() {
  var _self = this;
  /*new RequestUtils()._read('purchaseitem', null, 'd.sampleId=' + this._options.id, null, null, null, null, function(result, params) { _self._purchaseItems = result.data;
                                                                                                                                      _self._verifyItemData.call(_self);
                                                                                                                                    }, {pos: DOMUtils.findPos(this._gui.save)});
*/
   var args = '&oid=' + this._options.id; 
   
   new RequestUtils()._mysql('v1purchaseitem_s', args, function(result, params) { _self._purchaseItems = result.data;
                                                                          _self._verifyItemData.call(_self);
                                                                        }, {php:'customphp/mysqlread.php'});   

};

HandlePageBackendSample.prototype._retrieveProducePurchaseItems = function() {
  var _self = this;
  var joins = '&selects=1' + '&purchaseitems=0' + '&preconusers=0' + '&events=0' + '&tracks=0' + '&produces=0' + '&notes=0';
  var args = '&s=9999&c=d.branch = \'' + '<?php echo ((is_array($_tmp=@BRANCH)) ? $this->_run_mod_handler('escape', true, $_tmp, 'javascript') : smarty_modifier_escape($_tmp, 'javascript')); ?>
' + '\'' + ' AND ' + 'd.pi_sampleId=' + this._options.id + joins;
  new RequestUtils()._mysql('produce', args, function(result, params) { _self._producePurchaseItems = result.data;
                                                                        _self._verifyItemData.call(_self);
                                                                      }, { php: Constant.FACTORY + '/customphp/produceread.php', pos: DOMUtils.findPos(this._gui.save) });
};

HandlePageBackendSample.prototype._updateAllItems = function() {
  var _self = this;
  for (var a = 0, al = this._packageItems.length; a < al; a++) {
    var packageItem = this._packageItems[a];
    if (packageItem.selects.length == 0) {
      for (var m = 0, ml = this._sample.attributes.length; m < ml; m++) {
        var attribute = this._sample.attributes[m];
        DlmanageUtils.packageChoose(packageItem, attribute, attribute.options[0]);
      }
    } else {
      DlmanageUtils.modifySelects(packageItem.selects, this._modifyAttributes, this._modifyOptions, this._deleteAttributes, this._deleteOptions);
    }
  }
  for (var b = 0, bl = this._purchaseItems.length; b < bl; b++) {
    var purchaseItem = this._purchaseItems[b];
    if (purchaseItem.selects.length == 0) {
      for (var m = 0, ml = this._sample.attributes.length; m < ml; m++) {
        var attribute = this._sample.attributes[m];
        DlmanageUtils.purchaseChoose(purchaseItem, attribute, attribute.options[0]);
      }
    } else {
      DlmanageUtils.modifySelects(purchaseItem.selects, this._modifyAttributes, this._modifyOptions, this._deleteAttributes, this._deleteOptions);
    }
  }
  
  this._psold = [];
  for (var e = 0, el = this._producePurchaseItems.length; e < el; e++) {
    var pitem = this._producePurchaseItems[e];
    for (var f = 0, fl = pitem.pi_selects.length; f < fl; f++) {
      this._psold[this._psold.length] = pitem.pi_selects[f];
    }
  }
  
  this._ps = []; 
  for (var c = 0, cl = this._producePurchaseItems.length; c < cl; c++) {
    var pItem = this._producePurchaseItems[c];
    if (pItem.pi_selects.length == 0) {
      for (var m = 0, ml = this._sample.attributes.length; m < ml; m++) {
        var attribute = this._sample.attributes[m];
        DlmanageUtils.produceChoose(pItem, attribute, attribute.options[0]);
      }
    } else {
      DlmanageUtils.modifySelects(pItem.pi_selects, this._modifyAttributes, this._modifyOptions, this._deleteAttributes, this._deleteOptions);
    }
    for (var d = 0, dl = pItem.pi_selects.length; d < dl; d++) {
      this._ps[this._ps.length] = pItem.pi_selects[d];
    }
  }
  
  for (var i = 0, il = this._purchaseItems.length; i < il; i++) {
    var item = this._purchaseItems[i];
    item.type = this._sample.type;
    item.model = this._sample.model;
    item.unit = this._sample.unit;
  }
  
  //item.pi_type 、item.pi_model 为Album Design 就不修改这两项
  //2011年9月19日 10:07:35 
  for (var j = 0, jl = this._producePurchaseItems.length; j < jl; j++) {
    var item = this._producePurchaseItems[j];
    item.pi_type = (item.pi_type == 'Album Design')? item.pi_type : this._sample.type;
    item.pi_model = (item.pi_model == 'Album Design')? item.pi_model : this._sample.model;;
    item.pi_unit = this._sample.unit;
  }
  var args = '&sp=' + JSON.stringify(this._sample) + '&ps=' + JSON.stringify(this._purchaseItems) + '&pi=' + JSON.stringify(this._packageItems);
  new RequestUtils()._custom('packagepurchase', args, function(result, params) { if (_self._producePurchaseItems.length == 0) {
                                                                                   history.back();
                                                                                 } else {
                                                                                   var func = function() { history.back(); };
                                                                                   if (_self._ps.length == 0) {
                                                                                     new RequestUtils()._write2([{n: 'produce',a: _self._producePurchaseItems, r: []}, {n: 'produceselect',a: [], r: _self._psold}], func, {pos: DOMUtils.findPos(_self._gui.save), php: Constant.FACTORY + '/customphp/mysqlwrite2.php'});
                                                                                   } else {
                                                                                     new RequestUtils()._write2([{n: 'produce',a: _self._producePurchaseItems, r: []}, {n: 'produceselect',a: _self._ps, r: []}], func,  {pos: DOMUtils.findPos(_self._gui.save), php: Constant.FACTORY + '/customphp/mysqlwrite2.php'});
                                                                                   }
                                                                                 }
                                                                               }, {php:'customphp/pdocustom.php',pos: DOMUtils.findPos(this._gui.save)});
};

//HandlePageBackendSample.prototype._updatePurchaseItems = function() {
//  var _self = this;
//  
//  for (var i = 0, il = this._purchaseItems.length; i < il; i++) {
//    var item = this._purchaseItems[i];
//    item.type = this._sample.type;
//    item.model = this._sample.model;
//    item.unit = this._sample.unit;
//  }
//  for (var j = 0, jl = this._producePurchaseItems.length; j < jl; j++) {
//    var item = this._producePurchaseItems[j];
//    item.pi_type = this._sample.type;
//    item.pi_model = this._sample.model;
//    item.pi_unit = this._sample.unit;
//  }
//  
//  var args = '&sp=' + JSON.stringify(this._sample) + '&pi=' + JSON.stringify(this._purchaseItems);
//  new RequestUtils()._custom('purchasesample', args, function(result, params) { if (_self._producePurchaseItems.length == 0) {
//                                                                                  history.back();
//                                                                                } else {
//                                                                                  new RequestUtils()._write('produce', _self._producePurchaseItems, [], function() {  }, {pos: DOMUtils.findPos(_self._gui.save), php: Constant.FACTORY + '/jsonwrite.php'});
//                                                                                } 
//                                                                              }, {pos: DOMUtils.findPos(this._gui.save)});
//
//};

//HandlePageBackendSample.prototype._updatePackageItems = function() {
//  var _self = this;
//  
//  for (var a = 0, al = this._packageItems.length; a < al; a++) {
//    var packageItem = this._packageItems[a];
//    DlmanageUtils.modifySelects(packageItem.selects, this._modifyAttributes, this._modifyOptions, this._deleteAttributes, this._deleteOptions);
//  }
//  var args = '&sp=' + JSON.stringify(this._sample) + '&pi=' + JSON.stringify(this._packageItems);
//  new RequestUtils()._custom('packagesample', args, function(result, params) {  }, {pos: DOMUtils.findPos(this._gui.save)});
//};

HandlePageBackendSample.prototype._updateSample = function() {
  var _self = this;
  new RequestUtils()._mwrite('v1sample_u', [this._sample], [], function() { history.back(); }, {pos: DOMUtils.findPos(this._gui.save)});
};